﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using Kugar.Core.ExtMethod;
using Kugar.UI.Web.AdminLTE.Helpers;
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace Kugar.UI.Web.AdminLTE.PageControls.PageForms
{
    public class PageForm_SummerNote:PageForm_SingleInputFieldBase
    {
        private string _value="";
        private string _uploadUrl = "";
        private int _row = 4;
        private string _height = "200px";
        private string _placeHolder = "";
        private bool _readonly = false;

        public PageForm_SummerNote(PageForm_Form form, ViewContext viewContext) : base(form, viewContext)
        {
            this.Css("webui-form_summernotefield summernoteeditor");

            base.AdminLTEUI.PluginUsaged.SummerNote = true;
        }

        public PageForm_SummerNote Value(string value)
        {
            _value = value;
            return this;
        }

        public PageForm_SummerNote Height(string height)
        {
            _height = height;
            return this;
        }

        public PageForm_SummerNote Height(int height)
        {
            _height = height + "px";
            return this;
        }

        public PageForm_SummerNote ReadOnly(bool @readonly = true)
        {
            _readonly = @readonly;

            return this;
        }

        public PageForm_SummerNote Placeholder(string placeholder)
        {
            _placeHolder = placeholder;
            return this;
        }

        public PageForm_SummerNote UploadUrl(string uploadUrl)
        {
            _uploadUrl = uploadUrl;
            return this;
        }

        protected override void BeforeRender(TextWriter writer)
        {
            //if (string.IsNullOrWhiteSpace(_uploadUrl))
            //{
            //    _uploadUrl = WebDaemon.GlobalConfig.EditorImageUploadUrl;
            //}

            if (!string.IsNullOrWhiteSpace(_uploadUrl))
            {
                this.Data("uploadurl", _uploadUrl.ToStringEx());
            }

            //if (!string.IsNullOrWhiteSpace(_height))
            //{
            this.Data("height", _height);
            //}
            

            this.Data("placeholder", _placeHolder.ToStringEx());

            base.BeforeRender(writer);
        }

        protected override void RenderContent(TextWriter writer)
        {
            ViewContext.Writer.WriteLine($"<textarea class=\"textarea\" row=\"{_row}\" {_readonly.If("readonly")} name=\"{_name}\">{_value}</textarea>");   
        }
    }
}
